import Vue from 'vue'
import Router from 'vue-router'
import HelloWord from '@/components/HelloWord'
import loginpage from '@/components/loginpage'
import partnerPage from '@/components/partnerPage'
import headerPage from '@/components/headerPage'
import devlopPage from '@/components/devlopPage'


Vue.use(Router)
let router= new Router({
  routes: [
     {path:'/home',component:HelloWord},
     {path:'/',component:HelloWord},
     {path:'/loginpage',component:loginpage},
     {path:'/partnerPage',component:partnerPage},
     {path:'/headerPage',component:headerPage},
     {path:'/devlopPage',component:devlopPage},

  ]
})
router.beforeEach((to,from,next)=>{
  let islogin = JSON.parse(localStorage.getItem('user') || '{}') 
  if(islogin.userName){
    let now = new Date()
    console.log(Date.parse(now));
    console.log(Date.parse(islogin.time));
    let timecha = Date.parse(now)-Date.parse(islogin.time)
    console.log(timecha);
    let  days = timecha/1000/60/60/24
    let daysrd = Math.floor(days)
    console.log('daysrd',daysrd);
    let  hours = timecha/1000/60/60-(24*days)
    console.log(hours);
    if(hours>0.5){
      localStorage.removeItem('user')
      next('/loginpage')
      alert("登录失效，请重新登录")
    }
    next()
    console.log("已登录");
  }else{
    if(to.path=='/loginpage'){
      console.log("请登录");
        next()
    }else{
      console.log("未登录，请先去登录页");
      alert("未登录，请先登录")
      next('/loginpage')
    }
  }
})
export default router
